package cn.ld.modules.demo.test.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import cn.ld.modules.demo.test.entity.LdOrderCustomer;
import cn.ld.modules.demo.test.entity.LdOrderMain;
import cn.ld.modules.demo.test.entity.LdOrderTicket;
import cn.ld.modules.demo.test.mapper.LdOrderCustomerMapper;
import cn.ld.modules.demo.test.mapper.LdOrderMainMapper;
import cn.ld.modules.demo.test.mapper.LdOrderTicketMapper;
import cn.ld.modules.demo.test.service.ILdOrderMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 订单
 * @Author: ld-boot
 * @Date:  2019-02-15
 * @Version: V1.0
 */
@Service
public class LdOrderMainServiceImpl extends ServiceImpl<LdOrderMainMapper, LdOrderMain> implements ILdOrderMainService {

    @Autowired
    private LdOrderMainMapper ldOrderMainMapper;
    @Autowired
    private LdOrderCustomerMapper ldOrderCustomerMapper;
    @Autowired
    private LdOrderTicketMapper ldOrderTicketMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(LdOrderMain ldOrderMain, List<LdOrderCustomer> ldOrderCustomerList, List<LdOrderTicket> ldOrderTicketList) {
        ldOrderMainMapper.insert(ldOrderMain);
        if (ldOrderCustomerList != null) {
            for (LdOrderCustomer entity : ldOrderCustomerList) {
                entity.setOrderId(ldOrderMain.getId());
                ldOrderCustomerMapper.insert(entity);
            }
        }
        if (ldOrderTicketList != null) {
            for (LdOrderTicket entity : ldOrderTicketList) {
                entity.setOrderId(ldOrderMain.getId());
                ldOrderTicketMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(LdOrderMain ldOrderMain, List<LdOrderCustomer> ldOrderCustomerList, List<LdOrderTicket> ldOrderTicketList) {
        ldOrderMainMapper.updateById(ldOrderMain);

        //1.先删除子表数据
        ldOrderTicketMapper.deleteTicketsByMainId(ldOrderMain.getId());
        ldOrderCustomerMapper.deleteCustomersByMainId(ldOrderMain.getId());

        //2.子表数据重新插入
        if (ldOrderCustomerList != null) {
            for (LdOrderCustomer entity : ldOrderCustomerList) {
                entity.setOrderId(ldOrderMain.getId());
                ldOrderCustomerMapper.insert(entity);
            }
        }
        if (ldOrderTicketList != null) {
            for (LdOrderTicket entity : ldOrderTicketList) {
                entity.setOrderId(ldOrderMain.getId());
                ldOrderTicketMapper.insert(entity);
            }
        }
    }

    /**
     * 一对多维护逻辑改造  LOWCOD-315
     * @param ldOrderMain
     * @param ldOrderCustomerList
     * @param ldOrderTicketList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCopyMain(LdOrderMain ldOrderMain, List<LdOrderCustomer> ldOrderCustomerList, List<LdOrderTicket> ldOrderTicketList) {
        ldOrderMainMapper.updateById(ldOrderMain);

        // 循环前台传过来的数据
        for (LdOrderTicket ticket:ldOrderTicketList){
            // 先查询子表数据库
            LdOrderTicket orderTicket = ldOrderTicketMapper.selectById(ticket.getId());
            if(orderTicket == null){
                // 当传过来的id数据库不存在时，说明数据库没有，走新增逻辑
                ticket.setOrderId(ldOrderMain.getId());
                ldOrderTicketMapper.insert(ticket);
                break;
            }
            if(orderTicket.getId().equals(ticket.getId())){
                // 传过来的id和数据库id一至时，说明数据库存在该数据，走更新逻辑
                ldOrderTicketMapper.updateById(ticket);
            }
        }
        for (LdOrderCustomer customer:ldOrderCustomerList){
            // 先查询子表数据库
            LdOrderCustomer customers = ldOrderCustomerMapper.selectById(customer.getId());
            if(customers == null){
                // 当传过来的id数据库不存在时，说明数据库没有，走新增逻辑
                customer.setOrderId(ldOrderMain.getId());
                ldOrderCustomerMapper.insert(customer);
                break;
            }
            if(customers.getId().equals(customer.getId())){
                //TODO 传过来的id和数据库id一至时，说明数据库存在该数据，走更新逻辑
                ldOrderCustomerMapper.updateById(customer);
            }
        }
        // 当跟新和删除之后取差集， 当传过来的id不存在，而数据库存在时，说明已删除，走删除逻辑
        List<LdOrderTicket> ldOrderTickets = ldOrderTicketMapper.selectTicketsByMainId(ldOrderMain.getId());
        List<LdOrderTicket> collect = ldOrderTickets.stream()
                .filter(item -> !ldOrderTicketList.stream()
                .map(e -> e.getId())
                .collect(Collectors.toList())
                .contains(item.getId()))
                .collect(Collectors.toList());
        // for循环删除id
        for (LdOrderTicket ticket:collect){
            ldOrderTicketMapper.deleteById(ticket.getId());
        }

        List<LdOrderCustomer> ldOrderCustomers = ldOrderCustomerMapper.selectCustomersByMainId(ldOrderMain.getId());
        List<LdOrderCustomer> customersCollect = ldOrderCustomers.stream()
                .filter(item -> !ldOrderCustomerList.stream()
                        .map(e -> e.getId())
                        .collect(Collectors.toList())
                        .contains(item.getId()))
                .collect(Collectors.toList());
        //TODO for循环删除id
        for (LdOrderCustomer c:customersCollect){
            ldOrderCustomerMapper.deleteById(c.getId());
        }
    }
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		ldOrderMainMapper.deleteById(id);
		ldOrderTicketMapper.deleteTicketsByMainId(id);
		ldOrderCustomerMapper.deleteCustomersByMainId(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			ldOrderMainMapper.deleteById(id);
			ldOrderTicketMapper.deleteTicketsByMainId(id.toString());
			ldOrderCustomerMapper.deleteCustomersByMainId(id.toString());
		}
	}

}
